package com.ctcs.service;

import com.ctcs.domain.Movie;
import com.ctcs.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by duyaming on 2017/8/29.
 */
@Service
public class MovieService {
    @Autowired
    private MovieRepository movieRepository;


    public List<Movie>  movieList(Integer page){
        int size=10;
        Sort sort=new Sort(Sort.Direction.ASC,"id");
        Pageable pageable= new PageRequest(page,size,sort);
        Page<Movie> moviePage=movieRepository.findAll(pageable);
        List<Movie> list=new ArrayList<>();
        Iterator<Movie> it=moviePage.iterator();
        while(it.hasNext()){
            Movie movie=(Movie)it.next();
            list.add(movie);
        }
        return list;
    }

    public  Movie findById(Integer id){
        return  movieRepository.findOne(id);
    }
}
